package com.tan.androidtest.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.tan.androidtest.R;
import com.tan.androidtest.fragment.ChildLazyFragment;
import com.tan.androidtest.fragment.ChildLazyFragment1;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试懒加载
 */

public class TestLazyFragmentActivity extends FragmentActivity {

    private TabLayout tb_title;
    private ViewPager vp_content;
    private String[] contentStrs = new String[]{"哈哈1", "哈哈2"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_lazy_fragment_layout);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        tb_title = findViewById(R.id.tb_title);
        vp_content = findViewById(R.id.vp_content);

        final List<Fragment> list = new ArrayList<>();
        list.add(new ChildLazyFragment());
        list.add(new ChildLazyFragment1());

        vp_content.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {


            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        tb_title.setupWithViewPager(vp_content);
        for (int i = 0; i < contentStrs.length; i++) {
            TabLayout.Tab tab = tb_title.getTabAt(i);
            tab.setText(contentStrs[i]);
        }


    }
}
